# -*- coding: utf-8 -*-
from datetime import datetime

import pandas as pd
from docxtpl import DocxTemplate
from docx2pdf import convert


def main(ID):
    path = '【2020级6学期成绩信息】专业排名/【2020级6学期成绩信息】专业排名信息.xlsx'  # 由"专业排名&班级排名生成脚本.py"生成的所有专业排名信息Excel
    data = pd.read_excel(path)  # 原始信息

    filtered_data = data[data['学号'] == ID].iloc[0]

    # 插入信息
    profs_rank = filtered_data['专业排名']
    name = filtered_data['姓名']
    sex = filtered_data['性别']
    college = filtered_data['上课院系']
    profs = filtered_data['专业名称']
    cls = filtered_data['班级名称']
    GPA = filtered_data['平均学分绩点']
    date = datetime.now().date().strftime("%Y年%m月%d日")

    tpl = DocxTemplate('tpl.docx')
    context = {
        'profs_rank': profs_rank,
        'ID': ID,
        'name': name,
        'sex': sex,
        'college': college,
        'profs': profs,
        'cls': cls,
        'GPA': GPA,
        'date': date
    }
    tpl.render(context)
    word_file = f'{ID}-{name}.docx'
    PDF_file = f'{ID}-{name}.pdf'
    tpl.save(word_file)
    # convert(word_file, PDF_file)  # 输出为PDF文件


if __name__ == '__main__':
    main(input('请输入学号: '))
